home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp® 2.0.1 Tutorial / Chapter 08 / UIconEdit.inc1.p < prev    next >
Encoding:
Text File  |  1990-10-25  |  8.3 KB  |  306 lines  |  [TEXT/MPS ]

  1. {Copyright © 1989 by Apple Computer, Inc.  All rights reserved.}
  2.  
  3.  
  4.  
  5. CONST
  6.     kIconHBits =        32;                            { Number of horizontal bits in a bitmap.}
  7.     kIconVBits =        32;                            { Number of vertical bits in a bitmap.    }
  8.  
  9.     kIconSizeInBytes =    kIconHBits * kIconVBits DIV 8;    { Number of bytes in an bitmap.        }
  10.     kIconSizeInLongs =    kIconSizeInBytes DIV 4;        { Number of long words in a bitmap.        }
  11.     kMaxLong =            kIconSizeInLongs - 1;        { Max. addressable long word in bitmap.    }
  12.     
  13.     
  14.     { Resource identifiers }
  15.     
  16.     kSeedIconId =        1000;                        { Id of the seed icon resource.            }
  17.     
  18.  
  19.  
  20. TYPE
  21.     LongArrayHdl =        ^LongArrayPtr;
  22.     LongArrayPtr =        ^LongArray;
  23.     LongArray =            ARRAY [0..kMaxLong] OF LONGINT;
  24.  
  25.  
  26.  
  27. {-------------------------------------------------------------------------------------------}
  28. {--------------------------------TIconApplication methods-----------------------------------}
  29. {-------------------------------------------------------------------------------------------}
  30.  
  31. PROCEDURE TIconApplication.IIconApplication(iconFileType: OSType);
  32.  
  33. BEGIN
  34.     IApplication(iconFileType);
  35. END;
  36.  
  37.  
  38.  
  39. {-------------------------------------------------------------------------------------------}
  40.  
  41. FUNCTION  TIconApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  42.  
  43. VAR
  44.     anIconDocument:        TIconDocument;
  45.  
  46. BEGIN
  47.     New(anIconDocument);                            { Create a TIconDocument object.        }
  48.     FailNIL(anIconDocument);                        { Make sure we were successful.            }
  49.     anIconDocument.IIconDocument;                    { Initialize it.                        }
  50.  
  51.     DoMakeDocument := anIconDocument;                { Return a reference to the document.    }
  52. END;
  53.     
  54.     
  55.     
  56.     
  57. {-------------------------------------------------------------------------------------------}
  58. {----------------------------------TIconDocument methods------------------------------------}
  59. {-------------------------------------------------------------------------------------------}
  60.  
  61. PROCEDURE TIconDocument.IIconDocument;
  62.  
  63. VAR anIconBitMap : TIconBitMap;
  64.  
  65. BEGIN
  66.     fIconBitMap := NIL;                                { Set this to NIL so that if IDocument    }
  67.                                                     { fails, TIconDocument.Free works okay.    }
  68.  
  69.     IDocument(kFileType,                             { This document's file type.            }
  70.               kSignature,                             { This document's creator.                }
  71.               kUsesDataFork,                         { This document does use the data fork    }
  72.               NOT kUsesRsrcFork,                    { …but doesn't use the resource fork.    }
  73.               NOT kDataOpen,                        { We don't want the data fork kept open    }
  74.               NOT kRsrcOpen);                        { …nor the resource fork.                }
  75.  
  76.     New(anIconBitMap);                                { Allocate a new icon bitmap.            }
  77.     FailNil(anIconBitMap);                            { Fail if we can't allocate the handle.    }
  78.     anIconBitMap.IIconBitMap;                        { Initialize it.                        }
  79.     
  80.     fIconBitMap := anIconBitMap;                    { Store a reference to it in a field.   }
  81. END;
  82.  
  83.  
  84.  
  85. {-------------------------------------------------------------------------------------------}
  86.  
  87. PROCEDURE TIconDocument.DoInitialState; OVERRIDE;
  88.     { This method is called to set the document's data to the "new" state, as when the user    }
  89.     { chooses to open a new document instead of an existing one.  We set the value of the     }
  90.     { document's icon bit map to that of a "seed" icon in our resource file.  That way we     }
  91.     { can the document's initial state simply by changing the "seed" icon resource.            }
  92.  
  93. VAR
  94.     seedIcon:        Handle;
  95.  
  96. BEGIN
  97. {$IFC qDebug}
  98.     ProgramBreak('Yep.  DoInitialState was actually called.');
  99. {$ENDC}
  100.  
  101.     seedIcon := GetIcon(kSeedIconId);                { Get the seed icon resource.            }
  102.     FailNilResource(seedIcon);
  103.     fIconBitMap.SetIconBitMap(seedIcon)
  104. END;
  105.  
  106.  
  107.  
  108. {-------------------------------------------------------------------------------------------}
  109.  
  110. PROCEDURE TIconDocument.Free; OVERRIDE;
  111.     
  112. BEGIN
  113.     FreeIfObject(fIconBitMap);                        { Dispose of the icon object if non-Nil.}
  114.  
  115.     INHERITED Free;
  116. END;
  117.  
  118.  
  119.  
  120. {-------------------------------------------------------------------------------------------}
  121.  
  122. PROCEDURE TIconDocument.DoMakeViews(forPrinting: boolean); OVERRIDE;
  123.  
  124. BEGIN
  125.     { Do nothing for now. }
  126. END;
  127.  
  128.         
  129.  
  130. {-------------------------------------------------------------------------------------------}
  131.  
  132. PROCEDURE TIconDocument.Fields (PROCEDURE DoToField (fieldName: Str255;
  133.                                                    fieldAddr: Ptr;
  134.                                                    fieldType: INTEGER)); OVERRIDE;
  135.  
  136. BEGIN
  137.     DoToField('TIconDocument', NIL, bClass);
  138.     DoToField('fIconBitMap', @fIconBitMap, bObject);
  139.  
  140.     INHERITED Fields(DoToField);
  141. END;
  142.  
  143.     
  144. {-------------------------------------------------------------------------------------------}
  145. {-----------------------------------TIconBitMap methods-------------------------------------}
  146. {-------------------------------------------------------------------------------------------}
  147.  
  148. PROCEDURE TIconBitMap.IIconBitMap;
  149.  
  150. BEGIN
  151.     fDataHandle := NewPermHandle(kIconSizeInBytes);    { Allocate a handle for the bitmap.        }
  152.     FailNil(fDataHandle);                            { Fail if we can't allocate the handle.    }
  153. END;
  154.  
  155.  
  156.  
  157. {-------------------------------------------------------------------------------------------}
  158.  
  159. PROCEDURE TIconBitMap.Free; OVERRIDE;
  160.  
  161. BEGIN
  162.     DisposIfHandle(fDataHandle);                    { dispose of icon data.                    }
  163. END;
  164.  
  165.  
  166.  
  167. {-------------------------------------------------------------------------------------------}
  168.  
  169. PROCEDURE TIconBitMap.SetIconBitMap(theBitMap : Handle);
  170.  
  171. BEGIN
  172.     BlockMove(theBitMap^, fDataHandle^,                { …then copy it into the document's     }
  173.                 kIconSizeInBytes)                    { …icon bitmap.                            }
  174. END;
  175.             
  176.             
  177.  
  178. {-------------------------------------------------------------------------------------------}
  179.  
  180. PROCEDURE TIconBitMap.Clear;
  181.  
  182. VAR
  183.     iconAsLongArray:    LongArrayHdl;
  184.     i:                    INTEGER;
  185.  
  186. BEGIN
  187.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  188.  
  189.     FOR i := 0 TO kMaxLong DO                        { Clear the bits 32 at a time.            }
  190.         iconAsLongArray^^[i] := 0;
  191. END;
  192.  
  193.  
  194.  
  195. {-------------------------------------------------------------------------------------------}
  196.  
  197. PROCEDURE TIconBitMap.Invert;
  198.  
  199. VAR
  200.     iconAsLongArray:    LongArrayHdl;
  201.     i:                    INTEGER;
  202.  
  203. BEGIN
  204.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  205.  
  206.     FOR i := 0 TO kMaxLong DO                        { Invert the bits 32 at a time.            }
  207.         iconAsLongArray^^[i] := BNOT(iconAsLongArray^^[i]);
  208. END;
  209.  
  210.  
  211.  
  212. {-------------------------------------------------------------------------------------------}
  213.  
  214. PROCEDURE TIconBitMap.IconBitToWordBit (iconBit: Point; VAR word, bit: INTEGER);
  215.     { This converts the given icon bit to a word and bit in an array of long words. }
  216.  
  217. VAR
  218.     bitNumber:        INTEGER;
  219.  
  220. BEGIN
  221.     bitNumber := iconBit.v * kIconVBits + iconBit.h;
  222.     word := bitNumber DIV 32;
  223.     bit := 31 - (bitNumber MOD 32);
  224. END;
  225.  
  226.  
  227.  
  228. {-------------------------------------------------------------------------------------------}
  229.  
  230. FUNCTION  TIconBitMap.GetBit (iconBit: Point): BOOLEAN;
  231.     { Returns the state of the given bit in the icon being drawn. }
  232.  
  233. VAR
  234.     word:            INTEGER;
  235.     bitInWord:        INTEGER;
  236.  
  237. BEGIN
  238.     IconBitToWordBit(iconBit, word, bitInWord);
  239.  
  240.     GetBit := BTst(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  241. END;
  242.  
  243.  
  244.  
  245. {-------------------------------------------------------------------------------------------}
  246.  
  247. PROCEDURE TIconBitMap.SetBit (iconBit: Point; turnBitOn: BOOLEAN);
  248.     { Set the state of the given bit in the icon being drawn. }
  249.  
  250. VAR
  251.     word:            INTEGER;
  252.     bitInWord:        INTEGER;
  253.  
  254. BEGIN
  255.     IconBitToWordBit(iconBit, word, bitInWord);
  256.  
  257.     {$H-}                                            { So the compiler thinks this is unsafe.}
  258.     IF turnBitOn THEN
  259.         BSet(LongArrayHdl(fDataHandle)^^[word], bitInWord)
  260.     ELSE
  261.         BClr(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  262.     {$H+}
  263. END;
  264.  
  265.  
  266.  
  267. {-------------------------------------------------------------------------------------------}
  268.  
  269. FUNCTION TIconBitMap.Copy: TIconBitMap;
  270.  
  271. VAR
  272.     copyOfIcon:        TIconBitMap;
  273.  
  274. BEGIN
  275.     New(copyOfIcon);                                { Create a TIcon object.                }
  276.     FailNIL(copyOfIcon);                            { Make sure we were successful.            }
  277.     copyOfIcon.IIconBitMap;                            { Initialize it.                        }
  278.     copyOfIcon.SetIconBitMap(fDataHandle);            { Copy the data.                        }
  279.     Copy := copyOfIcon;                                { Return a reference to the new handle.    }
  280. END;
  281.  
  282.  
  283.             
  284. {-------------------------------------------------------------------------------------------}
  285.  
  286. PROCEDURE TIconBitMap.CopyDataTo (anIcon: TIconBitMap);
  287.  
  288. BEGIN
  289.     anIcon.SetIconBitMap(fDataHandle);                { Copy data to the new icon.            }
  290. END;
  291.  
  292.  
  293. {-------------------------------------------------------------------------------------------}
  294.  
  295. PROCEDURE TIconBitMap.Fields (PROCEDURE DoToField (fieldName: Str255;
  296.                                                    fieldAddr: Ptr;
  297.                                                    fieldType: INTEGER)); OVERRIDE;
  298.  
  299. BEGIN
  300.     DoToField('TIconBitMap', NIL, bClass);
  301.     DoToField('fDataHandle', @fDataHandle, bHandle);
  302.  
  303.     INHERITED Fields(DoToField);
  304. END;
  305.  
  306.